/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-10-24
 * Time: 18:55
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode(int val) {
        this.val = val;
    }
}
public class BinarySearchTree {
    public TreeNode root = null;

    //查找节点
    public TreeNode search(int key){
        TreeNode cur = root;
        while (cur != null){
            if (cur.val > key){
                cur = cur.left;
            }else if (cur.val < key){
                cur = cur.right;
            }else{
                return cur;
            }
        }
        return null;
    }

    //插入节点
    public boolean insert(int val){
        if (root == null){
            root = new TreeNode(val);
            return true;
        }

        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null){
            parent = cur;
            if (cur.val < val){
                cur = cur.right;
            }else if (cur.val > val){
                cur = cur.left;
            }else{
                return false;
            }
        }
        TreeNode node = new TreeNode(val);
        if (parent.val > val){
            parent.left = node;
        }else{
            parent.right = node;
        }
        return true;
    }

    //删除数据
    public boolean remove(int key){
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null){
            if (cur.val == key){
                removeTreeNode(cur, parent);
                break;
            }else if (cur.val < key){
                parent = cur;
                cur = cur.right;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
        if (cur == null){
            return false;
        }
        return true;
    }

    public void removeTreeNode(TreeNode cur, TreeNode parent){
        if (cur.left == null){
            if (cur == root){
                root = cur.right;
            }else if (cur == parent.left){
                parent.left = cur.right;
            }else{
                parent.right = cur.right;
            }
        }else if (cur.right == null){
            if (cur == root){
                root = cur.left;
            }else if (cur == parent.left){
                parent.left = cur.left;
            }else{
                parent.right = cur.left;
            }
        }else{
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while (target.left != null){
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;

            if (target == targetParent.left){
                targetParent.left = target.right;
            }else{
                targetParent.right = target.right;
            }
        }
    }


    public void inOrder(TreeNode root){
        if (root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    public static void main(String[] args) {
        int[] array = {10,8,19,3,9,4,7};
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        for (int i = 0; i < array.length; i++) {
            binarySearchTree.insert(array[i]);
        }
        binarySearchTree.inOrder(binarySearchTree.root);
        System.out.println();
        System.out.print("插入重复数据 ");
        System.out.println(binarySearchTree.insert(3));
        System.out.println("删除数据");
        binarySearchTree.remove(7);
        binarySearchTree.inOrder(binarySearchTree.root);
    }
}
